﻿using System;
using System.Data;
using System.IO;
using WebUI.BL.ProductCategories;
using WebUI.BL.Products;
using WebUI.BL.ProductSizeBL;
using WebUI.Lang;
using WebUI.Models;

namespace WebUI
{
    public partial class ProductCategory : AdlerBasePage
    {
        WebFunction func = new WebFunction();
        DataTable dtcolor;
        DataTable dtsize;

        public string displaylink = "";
        public string displayimg = "";
        public string displayName = "";
        public string bncategory = "";

        public string category = "0";
        public string page = "1";
        public string pagesize = "12";
        public int npage = 1;
        public int npagesize = 12;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                getBanner();
            }
        }

        private void BindData()
        {
            ProductColorBL procolor = new ProductColorBL();
            ProductSizeBL prosize = new ProductSizeBL();

            dtcolor = procolor.Get_ColorAll();
            func.LoadDataToDropDownList(ddlcolor, dtcolor, "ColorNo", "ColorName", true);

            dtsize = prosize.Get_SizeAll();
            func.LoadDataToDropDownList(ddlsize, dtsize, "SizeNo", "SizeName", true);

            category = (string)Request.QueryString["cat"];
            page = (string)Request.QueryString["page"];
            pagesize = (string)Request.QueryString["pagesize"];
            ProductCategories repository = new ProductCategories();            

            if (category != null && category != "")
            {
                int categoryno = Convert.ToInt32(category);
                if(page != null && page != "")
                    npage = Convert.ToInt32(page);

                if (pagesize != null && pagesize != "")
                    npagesize = Convert.ToInt32(pagesize);

                rptProductCategory.DataSource = repository.Get_Font_Category(categoryno, npage, npagesize);
                rptProductCategory.DataBind();
            }
            else
                Response.Redirect("http://www.adler.vn");
        }

        private void getBanner()
        {
            WebComponent component = new WebComponent();

            string bannercategory = "~/Uploads/XML/bannerCategory.xml";
            // Banner Look book
            using (DataSet dsmain = new DataSet())
            {
                if (!File.Exists(Server.MapPath(bannercategory))) return;
                dsmain.ReadXml(Server.MapPath(bannercategory));
                if (dsmain.Tables[0].Rows.Count > 0)
                {
                    displaylink = dsmain.Tables[0].Rows[0]["DisplayLink"].ToString();
                    displayimg = dsmain.Tables[0].Rows[0]["DisplayImg"].ToString();
                    displayName = dsmain.Tables[0].Rows[0]["DisplayName"].ToString();
                    bncategory = "<a href=\"" + displaylink + "\" title=\"" + displayName + "\"><img src=\"" + displayimg + "\" style=\"width:980px; height:250px;\" alt=\"" + displayName + "\" /></a>";
                }
            }
        }

        protected void ddlcolor_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlsize_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}